package com.newsproject.controller;

import com.newsproject.pojo.*;
import com.newsproject.pojo.vo.PublishPost;
import com.newsproject.pojo.vo.CommentVo;
import com.newsproject.pojo.vo.NewsRequest;
import com.newsproject.service.MinioService;
import com.newsproject.service.PostService;
import io.swagger.annotations.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/NewsController")
@CrossOrigin
public class NewsController {
    private static final Log log = LogFactory.getLog(NewsController.class);
    @Autowired
    private PostService postService;

    @GetMapping("/{postId}")
    @ApiResponses(value = {@ApiResponse(code = 200 ,message = "comments: 是一个集合，查询的4条评论,post: 是一个对象，查询的新闻咨询",response = CommentVo.class),
            @ApiResponse(code = 200,message = "post: 是一个对象，查询的新闻咨询",response = Post.class)
    })
    @ApiOperation(value = "获取新闻资讯", notes = "需要传第新闻咨询的id")
    public ModelAndView getEssayPage(@PathVariable @ApiParam("新闻咨询的id") Integer postId,
                                     @ApiParam("获取用户信息") HttpSession session){
        return   postService.getEssayPage(postId,session);
    }

    @ApiIgnore
    @GetMapping("login")
    public String login(){
        return "login";
    }
    @GetMapping("/toPost")
    public String register(){
        return "postEssay";
    }


    @ApiOperation(value = "转发新闻",notes = "需要用户Id和新闻id")
    @PostMapping("/sharePost")
    @ResponseBody
    public Result<Object> shareNews(@ApiParam("当前新闻id") @RequestParam int postId,@ApiParam("被转发的用户id")@RequestParam int forwardedId, @ApiIgnore HttpSession session) {
        return postService.sharePost(postId,forwardedId,session);
    }

    @ApiOperation(value = "关注,关注过再点击则取消",notes="需要关注者的Id和被关注者的Id")
    @PostMapping("/follow")
    @ResponseBody
    public  Result<Object> follow(@ApiParam("被关注者的id")@RequestParam int followedId,
                                  @ApiIgnore HttpSession session){
        return postService.follower(followedId,session);
    }


    @ApiOperation(value = "新闻点赞", notes = "点赞后再次点赞就取消点赞")
    @PostMapping("/likepost")
    @ResponseBody
    public Result<Integer> likepost(@ApiParam(value = "当前新闻ID")@RequestParam("postId")int postId, @ApiIgnore HttpSession session)
    {
        return postService.intserHeart(postId,session);
    }

    @ApiOperation(value = "添加评论点赞并返回点赞数", notes = "点赞过再次点赞就取消点赞")
    @PostMapping("/likecomment")
    @ResponseBody
    public Result<Object> likecomment(@RequestParam("commentId")int commentId, @ApiIgnore HttpSession session)throws Exception
    {
        return  postService.insertLike(commentId,session);
    }
    @ApiOperation(value = "收藏新闻", notes = "收藏新闻")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "收藏成功"),
            @ApiResponse(code = 400, message = "收藏失败")
    })
    @PostMapping("/bookmark")
    @ResponseBody
    public Result<Object> insertbookmark(@RequestParam("postId")int postId,
                                         @ApiIgnore HttpSession session)
    {
        return postService.intserbookmark(postId,session);
    }

    @ApiOperation(value = "返回关注数")
    @GetMapping("/countfollower")
    @ResponseBody
    public Result<Object> countfolloer(HttpSession session)
    {
        return postService.countfollower(session);
    }
    @ApiOperation(value = "返回粉丝数")
    @GetMapping("/countfollowed")
    @ResponseBody
    public Result<Object> countfolloed(HttpSession session)
    {
        return postService.countfollowed(session);
    }
    @ApiOperation(value = "返回用户的作品总获赞数")
    @GetMapping("/allLike/{userId}")
    @ResponseBody
    public Integer countAllLike(@ApiParam("当前用户id")@PathVariable Integer userId)
    {
     return postService.countAlllike(userId);
    }

@ApiOperation(value = "复制转发链接")
@ResponseBody
@GetMapping("/repostUrl")
public Result<Object> repostUrl(@ApiIgnore HttpServletRequest request)
{
    return postService.getCurrentUrl(request);
}


    @ApiOperation("删除新闻咨询")
    @PostMapping("/deletePost/{postId}")
    @ResponseBody
    public String deletePost(@PathVariable Integer postId) {
        return  postService.deletePost(postId);
    }



    @PostMapping("/addNews")
    @ResponseBody
    public Result<?> post(@ModelAttribute PublishPost post,
                          @RequestPart("coverImage") MultipartFile coverImage,
                          @RequestPart(value = "picture",required = false) MultipartFile picture,
                          HttpSession session) throws Exception {
        return postService.post(post,coverImage,picture,session);
    }

}
